package Dbasea;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;

public class DatuBasea {

	private static DatuBasea instance = null;
	
	public static DatuBasea getInstance() {
		if (instance == null) {
			instance = new DatuBasea();
		}
		return instance;
	}
	
	private Connection konexioa = null;
	
	private DatuBasea() {
		try {
			this.konektatu();
		}
		catch (Exception e) {
		}
	}
	
	private void konektatu() throws ClassNotFoundException, SQLException{						    
		Class.forName("org.sqlite.JDBC");
		konexioa = DriverManager.getConnection("jdbc:sqlite:Db/OpenSecur.s3db");
	}
		
	public void itzali() throws SQLException {
		konexioa.close();
	}
	
	public boolean balidatuErabiltzailea(String Erab, String Pw) throws SQLException {		
		Statement stmt = konexioa.createStatement();
		String query = "SELECT * FROM Idazkariak WHERE izena='"+Erab+"' AND password ='"+Pw+"'";
		ResultSet rs = stmt.executeQuery(query);
		boolean aurki= rs.next();		
		stmt.close();
		return aurki;	
	}

	public boolean balioEmail(String email) throws SQLException {		
		Statement stmt = konexioa.createStatement();
		String query = "SELECT * FROM Idazkariak WHERE email='"+email+"'";
		ResultSet rs = stmt.executeQuery(query);
		boolean aurki = rs.next();		
		stmt.close();
		return aurki;	
	}
			
	public String lortuMail(String mail) throws SQLException {
		Statement stmt = konexioa.createStatement();
		String K1 = "SELECT password FROM Idazkariak WHERE email='" + mail+ "'";
		ResultSet rs = stmt.executeQuery(K1);
		try {
			rs.next();
			return rs.getString("password");
		} catch (SQLException e) {
			// EMAITZA HUTSA
			e.printStackTrace();
		}
		return null;
	}	
	
	    		
}
